package com.ruoyi.hr.service.impl.welfare.welfareopen;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfareopen.WelfareOpen;
import com.ruoyi.hr.mapper.welfare.welfareopen.WelfareOpenMapper;
import com.ruoyi.hr.service.welfare.welfareopen.WelfareOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WelfareOpenServiceImpl implements WelfareOpenService {

    @Autowired
    WelfareOpenMapper welfareOpenMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareOpen> findData(WelfareOpen welfareOpen, List<Authority> authorities, int isAdmin) {
        return welfareOpenMapper.findData(welfareOpen, authorities, isAdmin);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public WelfareOpen findWelfareOpen(int id) {
        return welfareOpenMapper.findWelfareOpen(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateWelfareOpen(WelfareOpen welfareOpen) {
        //有没有社保数据
        int sb = welfareOpenMapper.findsbCount(welfareOpen.getId());
        if (sb > 0) {
            welfareOpenMapper.updatesbXvalue(welfareOpen);
        } else {
            welfareOpenMapper.insertsbXvalue(welfareOpen);
        }

        //有没有公积金
        int gjj = welfareOpenMapper.findgjjCount(welfareOpen.getId());
        if (gjj > 0) {
            welfareOpenMapper.updategjjXvalue(welfareOpen);
        } else {
            welfareOpenMapper.insertgjjXvalue(welfareOpen);
        }

        return welfareOpenMapper.updateWelfareOpen(welfareOpen);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<List<Map<String, Object>>> executeProcedure(String sql) {
        return welfareOpenMapper.executeProcedure(sql);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareOpen> findDataByIds(String ids) {
        return welfareOpenMapper.findDataByIds(ids);
    }
}
